package top.cjwtx.holy.phone.config;

import lombok.extern.log4j.Log4j2;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import top.cjwtx.holy.config.common.C;
import top.cjwtx.holy.pojo.dto.UserDTO;
import top.cjwtx.holy.service.IUserService;

/**
 * shiro自定义实现realm
 */
@Log4j2
public class UserRealm extends AuthorizingRealm {

    @Autowired
    private IUserService userService;

    /**
     * 授权
     *
     * @param principal
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principal) {
        log.info("授权..=============================================================");
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        UserDTO user = (UserDTO) principal.getPrimaryPrincipal();
        if (null == user) {
            log.error("授权失败，用户信息为空！！！");
            return null;
        }
//        System.out.println("权限配置-->MyShiroRealm.doGetAuthorizationInfo()");
//        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
//        User userInfo  = (User)principal.getPrimaryPrincipal();
//        for(SysRole role:userInfo.getRoleList()){
//            authorizationInfo.addRole(role.getRole());
//            for(SysPermission p:role.getPermissions()){
//                authorizationInfo.addStringPermission(p.getPermission());
//            }
//        }
        return simpleAuthorizationInfo;
    }

    /**
     * 认证
     *
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //自己实现的认证类
        MyAuthenticationToken token = (MyAuthenticationToken) authenticationToken;
        //   UsernamePasswordToken token= (UsernamePasswordToken) authenticationToken;
        //     String tokenId=token.getToken();
        UserDTO userDTO = null;
        //  if(tokenId!=null){
        //token登陆
        //     }else {
        //账号密码登陆
        userDTO = userService.findUserDTOByUserNameAndType(token.getUsername(), C.ACCOUNT_TYPE_PHONE);
        //    }
        //-------登陆开始----------
        //没有这个用户
        if (userDTO == null) {
            throw new UnknownAccountException("账号密码错误！请重新输入");
        }
        // String passWord = String.valueOf(token.getPassword());

        //密码错误
//        if(!userAndAccount.getUserAccount().getPassWord().equals(passWord)) {
//            throw new UnknownAccountException("账号密码错误！请重新输入");
//        }
         //全站账号异常 //用户状态0未激活，1正常，2封禁
        if (userDTO.getState() == null || userDTO.getState() == 2) {
            throw new UnknownAccountException("账号被封禁，请联系客服或管理员！");
        }

        //单账号异常 //用户状态0未激活，1正常，2封禁
        if(userDTO.getUserAccount().getState()==null||userDTO.getUserAccount().getState()==2){
            throw new UnknownAccountException("账号被封禁，请联系客服或管理员！");
        }

        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(userDTO, userDTO.getUserAccount().getPassword(), getName());
        return info;
    }

    //自定义认证实现类MyAuthenticationToken才使用
    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof MyAuthenticationToken;
    }

}
